/*
 * chardev.c: Creates a read-only char device that says how many times
 * you have read from the dev file
 */

#include <linux/atomic.h>
#include <linux/cdev.h>
#include <linux/delay.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/kernel.h>	/* for sprintf() */
#include <linux/module.h>
#include <linux/printk.h>
#include <linux/version.h>
#include <linux/poll.h>

#define SUCCESS 0
#define DEVICE_NAME "chardev"
#define BUF_LEN 80
static int major;
enum {
    CDEV_NOT_USED,
    CDEV_EXCLUSIVE_OPEN,
};

/* Is device open? Used to prevent multiple access to device */
static atomic_t already_open = ATOMIC_INIT(CDEV_NOT_USED);
static char data[16];
static const int DataLen = sizeof(data);
static wait_queue_head_t read_queue;
static struct class* cls;
static int data_read = 1;

static int device_open(struct inode* inode, struct file* file)
{
    static int counter = 0;

    if (atomic_cmpxchg(&already_open, CDEV_NOT_USED, CDEV_EXCLUSIVE_OPEN))
        return -EBUSY;

    printk("I already told you %d times Hello world!\n", counter++);
    try_module_get(THIS_MODULE);

    return SUCCESS;
}

static int device_release(struct inode* inode, struct file* file)
{
    atomic_set(&already_open, CDEV_NOT_USED);
    module_put(THIS_MODULE);

    return SUCCESS;
}

static ssize_t device_read(struct file* filp,
    char __user* buffer, size_t length, loff_t* offset)
{
    int ret = 0;
    int len = length;
    int real_data_len;

    real_data_len = strlen(data);
    if (length > real_data_len)
        len = real_data_len;
    ret = copy_to_user(buffer, data, len);
    if (ret) {
        return -EFAULT;
    }
    ret = len;
    pr_info("read ret=%d, data=%s\n", ret, data);
    memset(data, 0, DataLen);
    data_read = 1;

    return len;
}

static ssize_t device_write(struct file* filp, const char __user* buf,
    size_t len, loff_t* off)
{
    int count = len;
    if (len > DataLen)
        count = DataLen - 1;
    if (copy_from_user(data, buf, count))
        return -EFAULT;
    // wake_up_interruptible(&read_queue);
    wake_up(&read_queue);
    data_read = 0;
    pr_info("write data=%s, len=%d\n", data, count);

    return count;
}

static unsigned int device_poll(struct file* filp, poll_table* wait)
{
    unsigned int mask = 0;
    poll_wait(filp, &read_queue, wait);
    if (!data_read && strlen(data) > 0)
        mask |= POLLIN | POLLRDNORM;
    pr_info("poll return data_read=%d, mask=%d\n", data_read, mask);
    return mask;
}

static struct file_operations chardev_fops = {
    .owner = THIS_MODULE,
    .open = device_open,
    .release = device_release,
    .read = device_read,
    .write = device_write,
    .poll = device_poll,
};

static int __init chardev_init(void)
{
    major = register_chrdev(0, DEVICE_NAME, &chardev_fops);
    if (major < 0) {
        pr_alert("Registering char device failed with %d\n", major);
        return major;
    }
    pr_info("I was assigned major number %d.\n", major);

#if LINUX_VERSION_CODE >= KERNEL_VERSION(6, 4, 0)
    cls = class_create(DEVICE_NAME);
#else
    cls = class_create(THIS_MODULE, DEVICE_NAME);
#endif
    device_create(cls, NULL, MKDEV(major, 0), NULL, DEVICE_NAME);
    init_waitqueue_head(&read_queue);
    pr_info("Device created on /dev/%s\n", DEVICE_NAME);

    return SUCCESS;
}

static void __exit chardev_exit(void)
{
    device_destroy(cls, MKDEV(major, 0));
    class_destroy(cls);

    /* Unregister the device */
    unregister_chrdev(major, DEVICE_NAME);
}

module_init(chardev_init);
module_exit(chardev_exit);

MODULE_LICENSE("GPL");

/* 测试
1.安装.ko: 
rmmod 13_wait_queue_poll_wait.ko; insmod 13_wait_queue_poll_wait.ko; chmod 0666 /dev/chardev


2.用户select/poll/epoll 示例
cd test; scons
./13_epoll_字符设备.out
1234
input: 1234
recv dev_fd: 1234,len=4

asdfg
input: asdfg
recv dev_fd: asdfg,len=5

556677
input: 556677
recv dev_fd: 556677,len=6

结果: epoll返回可读有效
 */